#include <QAbstractTableModel>
#include <QMutexLocker>
#include <QThread>
#include "model-result.h"

ModelResult::ModelResult(QObject *parent) :
    AbstractResult(parent), m_model(0)
{
}

const QAbstractTableModel *ModelResult::model() const
{
    QMutexLocker lock(m_mutex);
    return m_model;
}

QAbstractTableModel *ModelResult::model()
{
    QMutexLocker lock(m_mutex);
    return m_model;
}

void ModelResult::setModel(QAbstractTableModel *model)
{
    QMutexLocker lock(m_mutex);
    Q_ASSERT(m_model == 0);
    if (QThread::currentThread() != this->thread())
    {
        QMetaObject::invokeMethod(this, "setModel", Qt::QueuedConnection, Q_ARG(QAbstractTableModel*, model));
        return;
    }
    m_model = model;
    m_model->setParent(this);
    emitResultReady();
}

void ModelResult::emitResultReady()
{
    QMetaObject::invokeMethod(this, "resultReady", Qt::QueuedConnection, Q_ARG(ModelResult*, this));
}
